package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.service.DeliveryClerkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/8/22 0022.
 */
@Controller
@RequestMapping(ControllerConstants.DISPATCCHER)
public class DispatcherWController extends WebBaseController {

    @Autowired
    private DeliveryClerkService deliveryClerkService;

    /**
     * 跳转配送人员列表
     *
     * @return
     */
    @RequestMapping("/findDispatcherIndex")
    public String findDispatcher() {
        return "taoshunda/dispatcher/dispatcherList";
    }

    /**
     * 跳转配送人员认证列表
     *
     * @return
     */
    @RequestMapping("/findAuthenticationIndex")
    public String findAuthentication() {
        return "taoshunda/dispatcher/authentication";
    }

    /**
     * 跳转配送人员未认证列表
     *
     * @return
     */
    @RequestMapping("/findNotIndex")
    public String findNot() {
        return "taoshunda/dispatcher/noAuthentication";
    }

    /**
     * 查询配送人员列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryDispatcherList")
    public void queryDispatcherList(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String name = getString(request, "name", "");
                String lastTime = getString(request, "lastTime", "");
                String created = getString(request, "created", "");
                Integer cityId = getInt(request, "cityId", 0);
                return deliveryClerkService.queryDispatcherList(name, lastTime, created, cityId, nowPage, pageSize);
            }
        });
    }

    /**
     * 查询配送员订单
     *
     * @param request
     * @param response
     */
    @RequestMapping("/dispatcherOrder")
    public void dispatcherOrder(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return deliveryClerkService.dispatcherOrder(id, nowPage, pageSize);
            }
        });
    }

    /**
     * 查询配送员明细
     *
     * @param request
     * @param response
     */
    @RequestMapping("/dispatcherOrderDetails")
    public void queryDispatcherDetails(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return deliveryClerkService.dispatcherOrderDetails(id, nowPage, pageSize);
            }
        });
    }

    /**
     * 修改配送员信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateDispatcherInfo")
    public void updateDispatcherInfo(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String account = getString(request, "account", "");
                String name = getString(request, "name", "");
                String address = getString(request, "address", "");
                String headPic = getString(request, "headPic", "");
                Integer provincesId = getInt(request, "provincesId", 0);
                Integer cityId = getInt(request, "cityId", 0);
                Integer areaId = getInt(request, "areaId", 0);
                return deliveryClerkService.updateDispatcherInfo(id, account, name, address, headPic, provincesId, cityId, areaId);
            }
        });
    }

    /**
     * 配送员禁用、启用
     * 删除
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateState")
    public void updateState(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer state = getInt(request, "disState", 0);
                return deliveryClerkService.updateState(id, state);
            }
        });
    }

    /**
     * 重置配送员密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/resetPassword")
    public void resetPassword(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String pwd = getString(request, "pwd", "");
                return deliveryClerkService.resetPassword(id, pwd);
            }
        });
    }

    /**
     * 查询认证
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryAuthentication")
    public void queryAuthentication(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String startCreate = getString(request, "startCreate", "");
                String endCreate = getString(request, "endCreate", "");
                String startTime = getString(request, "startTime", "");
                String endTime = getString(request, "endTime", "");
                String name = getString(request, "name", "");
                Integer cityId = getInt(request, "cityId", 0);
                return deliveryClerkService.queryAuthentication(startCreate, endCreate, startTime, endTime, name, cityId, nowPage, pageSize);
            }
        });
    }

    /**
     * 查询未认证
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryNoAuthentication")
    public void queryNoAuthentication(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String name = getString(request, "name", "");
                Integer cityId = getInt(request, "cityId", 0);
                String startCreate = getString(request, "startCreate", "");
                String endCreate = getString(request, "endCreate", "");
                String startTime = getString(request, "startTime", "");
                String endTime = getString(request, "endTime", "");
                return deliveryClerkService.queryNoAuthentication(name, startCreate, endCreate, startTime, endTime, cityId, nowPage, pageSize);
            }
        });
    }

    /**
     * 配送员通过认证
     *
     * @param request
     * @param response
     */
    @RequestMapping("/throughDispatcher")
    public void throughDispatcher(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer isCertificate = getInt(request, "isCertificate", 0);
                return deliveryClerkService.throughDispatcher(id, isCertificate);
            }
        });
    }
}
